import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
// 引入后必须使用vuex
Vue.use(Vuex)

export default new Vuex.Store({
	// 数据状态
	state:{
		currentuser:null,
		isLogin:false,
		pizzadata:{},      		// pizzadata 数据状态
		curPlayList:[],         // 当前播放列表
		curPlaySong:'',   		// 当前播放歌曲名称
		curPlaySrc:'',			// 当前播放歌曲路径
		SongIndex:0,       		// 当前播放歌曲的索引
		palyState:true,         // 当前歌曲的播放状态
		audio:null,				// Dom 节点
		Songduration:0 		    // 歌曲时长
	},
	getters:{
		// playstate(state){
		// 	return state.curPlaySong;
		// },
		songName(state){
			return state.curPlaySong;
		},
		Songduration(state){
			return state.Songduration;
		},
		Audio(state){
			return state.audio;
		}
	},
	// 改变数据状态
	mutations:{
		setpizaadata(state,data){
			state.pizzadata = data;
		},
		addPizza(state,data){
			state.pizzadata.push(data);
		},
		removePizza(state,data){
			state.pizzadata.forEach((item,index) => {
				if (item == data) {
					state.pizzadata.splice(index,1);
				}
			})
		},
		checkLogin(state,data){
			state.isLogin = true;
			state.currentuser = data;
		},
		localislogin(state,y){
			alert(y);
			state.isLogin = y;
		},
		Out(state){
			state.currentuser = null;
			state.isLogin = false;
		},
		// 设置播放列表
		setPlayList(state,data){
			state.curPlayList = data;
			state.SongIndex = 0;       /*将新的播放列表索引设置为0;*/
		},
		// 设置audio Dom节点
		setAudio(state,audio){
			state.audio = audio;
		},
		// 设置当前歌曲的资源路径
		setaudioSrc(state,SongId){
			axios.get("/cloudmusic/?type=song&id=" + SongId).then((res)=>{
				console.log(res.data.data[0].url);
				state.audio.src = state.curPlaySrc = res.data.data[0].url;
			});
		},
		// 设置播放状态
		setPlayState(state){
			if (state.audio!==null && state.curPlaySrc !== '') {
				// console.log(state.audio.duration);
				state.palyState=!state.palyState;
				if (state.palyState) {
					state.audio.play();
				}else{
					state.audio.pause();
				}
			}else{
				state.palyState = false;
				return;
			}
		},
		// 设置当前歌曲名
		setcurPlaySong(state,curSong){
			state.curPlaySong = curSong;
		},
		// 设置当前歌曲的时长
		setSongduration(state,audio){
			state.Songduration = state.audio.duration;
		},
		// 设置当前歌曲所在列表的索引
		setcurIndex(state,curIndex){
			const listTotal = state.curPlayList.length;
			if(curIndex < 0) {
				curIndex = listTotal - 1;
			}
			else if(curIndex >= listTotal) {
				curIndex = 0;
			}
			state.SongIndex = curIndex;
			console.log(curIndex);

		},
		
		
		
	},
	actions:{
		checkAction({commit},localuser){
			commit('checkLogin',localuser);
			// state.isLogin = false;
		},
		changeSong(content,curIndex){
			content.commit('setcurIndex',curIndex);
			content.commit('setcurPlaySong',content.state.curPlayList[content.state.SongIndex].name);
			content.commit('setaudioSrc',content.state.curPlayList[content.state.SongIndex].id);
			content.commit('setSongduration',content.state.audio);
		},
	}

})

/*

1. Array.from(arr[],function(val){映射})
2. Array.isArray(object)  		返回布尔值
3. Array.of(arg1,arg2...) 		传入参数返回一个数组
4. arr.concat(arr1.arr2,...)    组合两个以上数组 返回一个新的数组
5. arr.filter((value,index) => {return value > 2})
  	  .findIndex(callback())    返回匹配到元素的第一个索引 或返回-1
  	  .indexOf("")				返回匹配到元素的索引
  	  .splice(index,1)			通过索引删除某个元素或多个
  	  .forEach(callback())
6. arr.indexOf("")              返回某个值在数组中第一个匹配的索引 或-1
7. arr.join("-")                以 - 连接数组中每个元素
8. arr.map(callback()) 			对数组的每个元素调用定义的回调函数并返回包含结果的数组
9. arr.pop()  arr.push()  arr.shift()  arr.unshift()


*/